<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="<%= path %>/public/extjs/resources/css/ext-all.css" />
<link rel="stylesheet" type="text/css" href="<%= path %>/public/css/icon.css" />
<link rel="stylesheet" type="text/css" href="<%= path %>/public/extux/LovCombo.css" />
</head>
<body topmargin="0">
<script type="text/javascript" src="<%= path %>/public/extjs/adapter/ext/ext-base.js"></script> 
<script type="text/javascript" src="<%= path %>/public/extjs/ext-all.js"></script> 
<script type="text/javascript" src="<%= path %>/public/extux/LovCombo.js"></script> 
<div id="main" style="margin:10 0 0 10;"></div>
<div id="main2" style="margin:10 0 0 10;"></div>
<script type="text/javascript" >
	Ext.BLANK_IMAGE_URL  =  '<%= path %>/public/extjs/resources/images/default/s.gif';
	Ext.QuickTips.init();
	var pathUrl = '<%= path %>'; //该变量必须存在
	Ext.onReady(function(){
		//列模型
		var sm = new Ext.grid.CheckboxSelectionModel();
		var editorC=new Ext.grid.GridEditor(new Ext.form.TextField({allowBlank:false}));
		var editorNum=new Ext.grid.GridEditor(new Ext.form.NumberField({allowBlank:false,maxValue:23}));
		var editorNum2=new Ext.grid.GridEditor(new Ext.form.NumberField({allowBlank:false,maxValue:59}));
		var editorNum3=new Ext.grid.GridEditor(new Ext.form.NumberField({allowBlank:false,maxValue:20}));
		//下拉框--begin
		var comboxData = [ ['D','日'],['M','月末'],['S','季末'],['Y','年末'] ];
		var comStore=  new Ext.data.SimpleStore({
	        fields:['value','disValue'],
	        data:comboxData
	  	});
		var editorBox=new Ext.form.ComboBox({
			id:'editorBox',
			store :comStore,
		    valueField :'value',
		    displayField :'disValue',
		    forceSelection :true,
		    typeAhead :true,
		    triggerAction :'all',
		    selectOnFocus :true,
		    allowBlank :false,
		    lazyRender : true,
		    mode: 'local'
		});
		function rendererCom(value, p, r) {  
			if(value=='D'){
				return '日';
			}else if(value=='M'){
				return '月末';
			}else if(value=='S'){
				return '季末';
			}else if(value=='Y'){
				return '年末';
			}
		}
		//下拉框--end
		//多选下拉框--bigin
		var lovStore=  new Ext.data.Store({
			proxy:new Ext.data.HttpProxy({
			    url:pathUrl+"/listFlows.do"
			}),
			reader:new Ext.data.JsonReader(
				{
					root:"list"},
				[{name:"flowId",mapping:"flowId"},
				 {name:"flowName",mapping:"flowName"}
				])
		});
		var lovBox=new Ext.ux.form.LovCombo({
			id:'lovBox',
			store :lovStore,
		    valueField :'flowId',
		    displayField :'flowName',
		    hideOnSelect:false,
		    forceSelection :true,
		    typeAhead :true,
		    triggerAction :'all',
		    selectOnFocus :true,
		    lazyRender : true
		});
		//多选下拉框--end
		//下拉框--begin
		var comStore=  new Ext.data.Store({
			proxy:new Ext.data.HttpProxy({
			    url:pathUrl+"/listRunParas.do"
			}),
			reader:new Ext.data.JsonReader(
				{
					root:"list"},
				[{name:"id",mapping:"id"},
				 {name:"paraName",mapping:"paraName"}
				])
		});
		comStore.load();
		var ediBox=new Ext.form.ComboBox({
			id:'ediBox',
			store :comStore,
		    valueField :'id',
		    displayField :'paraName',
		    forceSelection :true,
		    typeAhead :true,
		    triggerAction :'all',
		    selectOnFocus :true,
		    allowBlank :false,
		    lazyRender : true,
		    emptyText:'请选择'
		});
		function rendrCom(value, p, r) {  
			var index = comStore.find(Ext.getCmp('ediBox').valueField, value); 
		    var record = comStore.getAt(index);  
		    if (record == null) {  
		        return value;  
		    } else {
		        return record.data.paraName;    
		    }  
		}
		//下拉框--end
		var columnModel=new Ext.grid.ColumnModel([
       		new Ext.grid.RowNumberer(),
			sm,
       		{header:"作业流ID",dataIndex:"flowId",width:60,sortable: true},
       		{header:"作业流名称",dataIndex:"flowName",width:160,sortable: true,editor:editorC},
       		{header:"依赖作业流ID",dataIndex:"dependId",width:110,sortable: true,editor:lovBox},
       		{header:"运行参数",dataIndex:"flowPara",width:100,sortable: true,editor:ediBox,renderer:rendrCom},
       		{header:"运行时间（小时）",dataIndex:"runHour",width:100,sortable: true,editor:editorC},
       		{header:"运行时间（分）",dataIndex:"runMinus",width:100,sortable: true,editor:editorC},
       		{header:"频率类型",dataIndex:"freType",width:70,sortable: true,editor:editorBox,renderer:rendererCom},
       		{header:"运行频率",dataIndex:"frecuncy",width:60,sortable: true,editor:editorNum},
       		{header:"数据执行延期(天)",dataIndex:"dataDelay",width:100,sortable: true,editor:editorNum3},
       		{header:"最大等待时间（分）",dataIndex:"maxWaitTime",width:110,sortable: true,editor:editorC},
       		{header:"备注",dataIndex:"bak",width:100,sortable: true,editor:editorC},
       		{header:"等待次数",dataIndex:"waitTimes",hidden:true},
       		{header:"平均等待时间",dataIndex:"avgWait",hidden:true},
       		{header:"成功运行次数",dataIndex:"runtimes",hidden:true},
       		{header:"平均运行时间",dataIndex:"avgRun",hidden:true}
       	]);
       	
        var record=new Ext.data.Record.create([
			{name:'flowId',type:'string'},
        	{name:'flowName',type:'string'},
        	{name:'dependId',type:'string'},
        	{name:'flowPara',type:'string'},
        	{name:'runHour',type:'string'},
        	{name:'runMinus',type:'string'},
        	{name:'frecuncy',type:'string'},
        	{name:'freType',type:'string'},
        	{name:'dataDelay',type:'string'},
        	{name:'maxWaitTime',type:'string'},
        	{name:'bak',type:'string'}
        ]);
		//工具条
       	var ttbar=new Ext.Toolbar({
			width:600
         });
      	//数据来源
		var store=new Ext.data.Store({
			proxy:new Ext.data.HttpProxy({
			    url:pathUrl+"/listFlows.do"
			}),
			reader:new Ext.data.JsonReader(
				{
					totalProperty:"count",
					successProperty:"ready",
					root:"list"},
				[
				 {name:"flowId",mapping:"flowId"},
				 {name:"flowName",mapping:"flowName"},
				 {name:"dependId",mapping:"dependId"},
				 {name:"flowPara",mapping:"flowPara"},
				 {name:"runHour",mapping:"runHour"},
				 {name:"runMinus",mapping:"runMinus"},
				 {name:"frecuncy",mapping:"frecuncy"},
				 {name:"dataDelay",mapping:"dataDelay"},
				 {name:"freType",mapping:"freType"},
				 {name:"maxWaitTime",mapping:"maxWaitTime"},
				 {name:"bak",mapping:"bak"},
	        	 {name:'waitTimes',mapping:'waitTimes'},
	        	 {name:'avgWait',mapping:'avgWait'},
	        	 {name:'runtimes',mapping:'runtimes'},
	        	 {name:'avgRun',mapping:'avgRun'}
				])
		});

		
		var gridTable=new Ext.grid.EditorGridPanel({
		  	title:'作业流列表',
		  	height : 300,
			width : 1000,
			bodyStyle:'padding:5px',
			loadMask : true,
			pruneModifiedRecords:true,
			store:store,
			cm:columnModel,
			sm:sm,
			tbar:[{
   					text:"增加",
   					handler:addRow
	   			},
	    		{
	   				text:"删除",
	   				handler:deletess
	   			},{
	   				text:"保存",
		   			handler:save
	   			}
   			],
   			bbar:new Ext.PagingToolbar({    
   	            pageSize: 10,    
   	            store: store,    
   	            displayInfo: true,    
   	            displayMsg: '显示第 {0} 条到 {1} 条记录，一共 {2} 条',    
   	            emptyMsg: "没有记录"   
   	        })
			
		});
		store.load({params:{start:0,limit:10}}); 
		function addRow(){
			var insertV={flowName:'',dependId:'',flowPara:'',runHour:'',runMinus:'',frecuncy:'',freType:'',dataDelay:'',maxWaitTime:'',bak:''};
			var re=new record(insertV);
			gridTable.stopEditing();
			store.insert(0,re);
			gridTable.startEditing(0,0);
			re.dirty=true;
			re.modified=insertV;
			if(store.modified.indexOf(re)==-1){
				store.modified.push(re);
			}
		}
		function save(){
			var m=store.getModifiedRecords();
			for(var row = 0;row<m.length;row++){
                var record = m[row].data;
                var flowName = record["flowName"];
                if(flowName==null||flowName==""){
                  Ext.Msg.alert('验证','[作业流名称]为必填项');
                  return ;
                }
                var flowPara = record["flowPara"];
                if(flowPara==null||flowPara==""){
                    Ext.Msg.alert('验证','[运行参数]为必填项');
                    return ;
                }
                var runHour = record["runHour"];
                if(runHour==null||runHour==""){
                    Ext.Msg.alert('验证','[运行时间(小时)]为必填项');
                    return ;
                }
                var runMinus = record["runMinus"];
                if(runMinus==null||runMinus==""){
                    Ext.Msg.alert('验证','[运行时间(分钟)]为必填项');
                    return ;
                }
                var frecuncy = record["frecuncy"];
                if(frecuncy==null||frecuncy==""){
                    Ext.Msg.alert('验证','[运行频率]为必填项');
                    return ;
                }
                var freType = record["freType"];
                if(freType==null||freType==""){
                    Ext.Msg.alert('验证','[频率类型]为必填项');
                    return ;
                }
                var dataDelay = record["dataDelay"];
                if(dataDelay==null||dataDelay==""){
                    Ext.Msg.alert('验证','[数据执行延期]为必填项');
                    return ;
                }
                var bak = record["bak"];
                if(bak==null||bak==""){
                    Ext.Msg.alert('验证','[备注]为必填项');
                    return ;
                }
                var bak = record["maxWaitTime"];
                if(bak==null||bak==""){
                    Ext.Msg.alert('验证','[最大等待时间]为必填项');
                    return ;
                }
            }
			store.commitChanges();
			var jsonArray=[];
			Ext.each(m,function(item){
				jsonArray.push(item.data);
			});
			Ext.Ajax.request({
				method:'post',
				params:{ datas: Ext.util.JSON.encode(jsonArray) },
				url:pathUrl+'/saveFlows.do',
				callback : function(options, success, response) {
					var json = Ext.util.JSON.decode(response.responseText);
					if (success) {
						if(json.success){
							Ext.MessageBox.alert('提示信息', "保存成功");
							store.reload();
							lovStore.reload();
						}else{
							Ext.MessageBox.alert('提示信息', json.message);
						}
					}else{
						 Ext.MessageBox.show({
		                    title: '失败',
		                    msg: '保存失败，请重试！',
		                    buttons: Ext.MessageBox.OK,
		                    icon: Ext.MessageBox.ERROR
		                });
					}
				},
	            waitMsg: '正在处理数据，请稍后……',
	            scope: this
			});
		}
		function deletess(){
			var selections=gridTable.getSelectionModel().getSelections();
			if(selections.length==0){
				 Ext.MessageBox.alert('提示信息','请选择要删除的记录.');
				 return;
			} 
			var ids="";
			for(var i=0;i<selections.length;i++){
				var row=selections[i];
				ids=ids+row.get("flowId")+"@";
			}
			Ext.Ajax.request({
				method:'post',
				params:{ ids:ids},
				url:pathUrl+'/deleteFlows.do',
				callback : function(options, success, response) {
					var json = Ext.util.JSON.decode(response.responseText);
					if (success) {
						if(json.success){
							Ext.MessageBox.alert('提示信息', "删除成功");
							store.reload();
						}else{
							Ext.MessageBox.alert('提示信息', json.message);
						}
					}
				},
	            waitMsg: '正在处理数据，请稍后……',
	            scope: this
			});
		}
		gridTable.render("main");
		var flowId;
		gridTable.store.on("load",function(){  
			gridTable.getSelectionModel().selectFirstRow(); 
			if( gridTable.getSelections().length>0){
				flowId = gridTable.getSelections()[0].get('flowId');
				store2.load({params:{start:0,limit:100,flowId:flowId}});
			}
	    });  
		gridTable.on('cellclick', function(store,e) { 
			flowId = gridTable.getSelections()[0].get('flowId');
			store2.load({params:{start:0,limit:100,flowId:flowId}});
	    });
		//--------------------------------------------------------------------
		var sm2 = new Ext.grid.CheckboxSelectionModel();
		//下拉框--begin
		var comStore2=  new Ext.data.Store({
			proxy:new Ext.data.HttpProxy({
			    url:pathUrl+"/listJobs.do"
			}),
			reader:new Ext.data.JsonReader(
				{
					root:"list"},
				[{name:"jobId",mapping:"jobId"},
				 {name:"jobName",mapping:"jobName"}
				])
		});
		comStore2.load();
		var editorBox2=new Ext.form.ComboBox({
			id:'editorBox2',
			store :comStore2,
		    valueField :'jobId',
		    displayField :'jobName',
		    forceSelection :true,
		    typeAhead :true,
		    triggerAction :'all',
		    selectOnFocus :true,
		    allowBlank :false,
		    lazyRender : true
		});
		//下拉框--end
		var columnModel2=new Ext.grid.ColumnModel([
       		new Ext.grid.RowNumberer(),
			sm,
			{header:"作业ID",dataIndex:"jobId",width:120,sortable: true,editor:editorBox2},
       		{header:"作业名称",dataIndex:"jobName",width:260,sortable: true},
       		{header:"备注",dataIndex:"bbak",width:100,sortable: true,editor:editorC},
       		{header:"执行顺序",dataIndex:"jobSeq",width:100,sortable: true,editor:editorNum2}
       	]);
		//数据来源
		var store2=new Ext.data.Store({
			proxy:new Ext.data.HttpProxy({
			    url:pathUrl+"/listJobsByFlow.do"
			}),
			reader:new Ext.data.JsonReader(
				{
					totalProperty:"count2",
					root:"flowList"},
				[
				 {name:"id",mapping:"id"},
				 {name:"jobId",mapping:"jobId"},
				 {name:"jobName",mapping:"jobName"},
				 {name:"jobType",mapping:"jobType"},
				 {name:"bbak",mapping:"bbak"},
				 {name:"jobSeq",mapping:"jobSeq"}
				])
		});
		var gridTable2=new Ext.grid.EditorGridPanel({
		  	title:'作业列表',
		  	height : 260,
			width : 800,
			bodyStyle:'padding:5px',
			loadMask : true,
			pruneModifiedRecords:true,
			store:store2,
			cm:columnModel2,
			sm:sm2,
			tbar:[{
   					text:"增加",
   					handler:addRow2
	   			},
	    		{
	   				text:"删除",
	   				handler:deletess2
	   			},{
	   				text:"保存",
		   			handler:save2
	   			}
   			],
   			bbar:new Ext.PagingToolbar({    
   	            pageSize: 10,    
   	            store: store2,    
   	            displayInfo: true,    
   	            displayMsg: '显示第 {0} 条到 {1} 条记录，一共 {2} 条',    
   	            emptyMsg: "没有记录"   
   	        })
			
		});
		gridTable2.render("main2");
		function addRow2(){
			var insertV={jobId:'',jobSeq:'',bbak:''};
			var re=new record(insertV);
			gridTable.stopEditing();
			store2.insert(0,re);
			gridTable2.startEditing(0,0);
			re.dirty=true;
			re.modified=insertV;
			if(store2.modified.indexOf(re)==-1){
				store2.modified.push(re);
			}
		}
		function save2(){
			var m=store2.getModifiedRecords();
			for(var row = 0;row<m.length;row++){
                var record = m[row].data;
                var jobId = record["jobId"];
                if(jobId==null||jobId==""){
                  Ext.Msg.alert('验证','[作业ID]为必填项');
                  return ;
                }
                var jobSeq = record["jobSeq"];
                if(jobSeq==null||jobSeq==""){
                    Ext.Msg.alert('验证','[执行顺序]为必填项');
                    return ;
                }
                var bak = record["bbak"];
                if(bak==null||bak==""){
                    Ext.Msg.alert('验证','[备注]为必填项');
                    return ;
                }
            }
			store2.commitChanges();
			var jsonArray=[];
			Ext.each(m,function(item){
				jsonArray.push(item.data);
			});
			Ext.Ajax.request({
				method:'post',
				params:{ datas2: Ext.util.JSON.encode(jsonArray),flowId:flowId },
				url:pathUrl+'/saveFlowJobs.do',
				callback : function(options, success, response) {
					var json = Ext.util.JSON.decode(response.responseText);
					if (success) {
						if(json.success){
							Ext.MessageBox.alert('提示信息', "保存成功");
							store2.reload();
						}else{
							Ext.MessageBox.alert('提示信息', json.message);
						}
					}else{
						 Ext.MessageBox.show({
		                    title: '失败',
		                    msg: '保存失败，请重试！',
		                    buttons: Ext.MessageBox.OK,
		                    icon: Ext.MessageBox.ERROR
		                });
					}
				},
	            waitMsg: '正在处理数据，请稍后……',
	            scope: this
			});
		}
		function deletess2(){
			var selections=gridTable2.getSelectionModel().getSelections();
			if(selections.length==0){
				 Ext.MessageBox.alert('提示信息','请选择要删除的记录.');
				 return;
			} 
			var ids="";
			for(var i=0;i<selections.length;i++){
				var row=selections[i];
				ids=ids+row.get("id")+"@";
			}
			Ext.Ajax.request({
				method:'post',
				params:{ ids2:ids},
				url:pathUrl+'/deleteFlowJobs.do',
				callback : function(options, success, response) {
					var json = Ext.util.JSON.decode(response.responseText);
					if (success) {
						if(json.success){
							Ext.MessageBox.alert('提示信息', "删除成功");
							store2.reload();
						}else{
							Ext.MessageBox.alert('提示信息', json.message);
						}
					}
				},
	            waitMsg: '正在处理数据，请稍后……',
	            scope: this
			});
		}
	});
</script>
</body>

</html>

